package com.emep.changzhi.analyse.http.outlet;

import com.emep.changzhi.analyse.entity.basics.SysAreaInfo;
import com.emep.changzhi.analyse.entity.basics.SysCompanyInfo;
import com.emep.changzhi.analyse.entity.outlet.SysOutletInfo;
import com.emep.changzhi.analyse.model.Result;
import com.emep.changzhi.analyse.model.ResultBean;
import com.emep.changzhi.analyse.service.outlet.SysOutletInfoService;
import com.emep.changzhi.analyse.utils.Constant;
import com.emep.changzhi.analyse.utils.DataValidator;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Wangcc
 * @version V1.0
 * @Description: 排口信息控制类
 * @date 2018/2/26 14:50
 */
@RestController
@RequestMapping("outlet")
@Api(value="排口信息controller",tags={"排口信息"})
public class SysOutletInfoController {
    
    @Autowired
    private SysOutletInfoService sysOutletInfoService;

    /**
     * 查询所有排口信息
     * @return
     */
    @ApiOperation(value="获取排口信息列表", notes="获取排口信息列表")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
                    @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
                    @ApiResponse(code = 303, message = "权限不足") })
    @GetMapping(value = "list")
    public ResultBean<List<SysOutletInfo>> queryUser(){
        return new ResultBean<List<SysOutletInfo>>(sysOutletInfoService.findAll());
    }

    /**
     * 新增排口信息
     * @param payload
     * @return
     */
    @ApiOperation(value="新增排口信息", notes="新增排口信息")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
                    @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
                    @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "修改时传的id", required = false, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "companyId", value = "企业iud", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "outletType", value = "排口类型", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "outletName", value = "排口名字", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "longitude", value = "排口经度", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "latitude", value = "排口纬度", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "outletStatus", value = "排口状态", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "outletAccess", value = "排放量数值获取方式", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "unifiedCode", value = "排口唯一编码", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "polluteType", value = "污染类型", required = true, dataType = "String",paramType="query"),
    })
    @PostMapping(value = "add")
    public ResultBean<SysOutletInfo> add(@RequestParam Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload, "companyId", "outletType", "outletName", "longitude",
                "latitude", "outletStatus", "outletAccess", "unifiedCode", "polluteType");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS){
            return result.boolAdd(sysOutletInfoService.save(payload));
        }
        return  result;
    }

    /**
     * 通过id 查询排口信息
     * @param payload
     * @return
     */
    @ApiOperation(value="根据id 查询排口信息", notes="根据id来获取排口信息")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
                    @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
                    @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParam(name = "id", value = "排口信息id", required = true, dataType = "String", paramType = "query")
    @GetMapping(value = "info")
    public ResultBean<SysOutletInfo> query( @RequestParam Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload, "id");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS) {
            return result.ok(sysOutletInfoService.findId(payload.get("id").toString()));
        }
        return result;
    }

    /**
     * 根据id 删除排口信息
     * @param payload
     */
    @ApiOperation(value="删除排口信息", notes="根据排口信息id删除排口信息")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
                    @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
                    @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParam(name = "id", value = "排口信息id", required = true, dataType = "String",paramType = "query")
    @DeleteMapping(value = "delete")
    public ResultBean<Boolean> delete(@RequestParam Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload, "id");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS){
            return result.bool(sysOutletInfoService.delete(payload.get("id").toString()));
        }
        return result;
    }

    /**
     * 分页查询排口信息列表
     * page，第几页，从0开始，默认为第0页
     * size，每一页的大小，默认为20
     * sort，排序相关的信息，以property,property(,ASC|DESC)的方式组织，例如sort=firstname&sort=lastname,desc表示在按firstname正序排列基础上按lastname倒序排列
     * @param
     * @return
     */
    @ApiOperation(value="分页查询角色菜单关联关系", notes="分页查询角色菜单关联关系")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
                    @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
                    @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "页数", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "pageSize", value = "数量", required = true, dataType = "String",paramType="query"),
    })
    @GetMapping("paging")
    public ResultBean showOutlets(@RequestParam Map<String, Object> payload) {
        ResultBean result = DataValidator.validateMapData(payload, "pageIndex","pageSize");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS){
            Page<SysOutletInfo> pageSysOutletInfo = sysOutletInfoService.findAll(Integer.parseInt(payload.get("pageIndex")
                    .toString()),Integer.parseInt(payload.get("pageSize").toString()));
            return result.ok(pageSysOutletInfo);
        }
        return result;
    }

    /**
     * 查询不同状态排口
     */
    @ApiOperation(value="查询不同状态排口", notes="查询不同状态排口")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "areaCode", value = "行政区划", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "ouletStatus", value = "排口状态", required = true, dataType = "String",paramType="query"),
    })
    @GetMapping("list/status")
    public ResultBean<List<SysOutletInfo>> findByStatus(@RequestParam Map<String, Object> payload) {
        ResultBean result = DataValidator.validateMapData(payload, "areaCode","ouletStatus");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS) {
            SysOutletInfo sysOutletInfo = new SysOutletInfo();
            sysOutletInfo.setOutletStatus(payload.containsKey("ouletStatus") ? payload.get("ouletStatus") != null ? payload.get("ouletStatus").toString() : "" : "");
            List<SysOutletInfo> sysOutletInfos = sysOutletInfoService.findByOutletStatus(sysOutletInfo, payload.get("areaCode").toString());
            return result.ok(sysOutletInfos);
        }
        return result;
    }

    /**
     * 获取企业排口的情况
     */
//    @ApiOperation(value="获取企业排口的情况", notes="获取企业排口的情况")
//    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
//            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
//            @ApiResponse(code = 303, message = "权限不足") })
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "companyId", value = "企业id", required = true, dataType = "String",paramType="query"),
//    })
//    @GetMapping("findByCompany")
//    public  ResultBean<List<SysOutletInfo>> findByCompany(@RequestParam Map<String, Object> payload){
//        ResultBean result = DataValidator.validateMapData(payload, "companyId");
//        if (result.getCode() == Constant.RESULT_CODE_SUCCESS) {
//            SysCompanyInfo sysCompanyInfo = new SysCompanyInfo();
//            sysCompanyInfo.setId(payload.get("companyId").toString());
//            List<SysOutletInfo> sysOutletInfos = sysOutletInfoService.findSysOutletInfosByCompany(sysCompanyInfo);
//            return result.ok(sysOutletInfos);
//        }
//        return result;
//    }



    /**
     * 获取企业排口的详情（不输入排口id为查询企业下的所有排口。输入排口id为查询企业下的某个排口的详细情况）
     */
    @ApiOperation(value="获取企业的排口的详情或企业下的所有排口状态", notes="获取企业的排口的详情或企业下的所有排口状态")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companyId", value = "企业id", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "outletId", value = "排口id", required = false, dataType = "String",paramType="query"),
    })
    @GetMapping("company/info")
    public  ResultBean<List<SysOutletInfo>> findByCompanyAndId(@RequestParam Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload, "companyId");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS) {
            SysCompanyInfo sysCompanyInfo = new SysCompanyInfo();
            sysCompanyInfo.setId(payload.get("companyId").toString());
            List<SysOutletInfo> sysOutletInfos = new ArrayList<>();
            if(payload.containsKey("outletId")){
                sysOutletInfos = sysOutletInfoService.findAllByCompanyAndId(sysCompanyInfo,payload.get("outletId").toString());
            }else {
                sysOutletInfos = sysOutletInfoService.findSysOutletInfosByCompany(sysCompanyInfo);
            }
            if(sysOutletInfos.size() != 0){
                return result.ok(sysOutletInfos);
            }else {
                return result.ok("无排口数据");
            }
        }
        return result;
    }
}
